package view;

import dao.customerDao;
import model.Customer;
import util.dbUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * @program: foodApp
 * @description:
 * @author: PanKer
 * @create: 2021-11-20 10:27
 */

@WebServlet("/customerVerification")
public class customerVerification extends HttpServlet {
    private static final long serialVersionUID = -4279278779621220391L;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        设置字符编码
//        获取用户名和密码
//        连接数据库验证用户名和密码
//        登录成功跳转到成功页面，失败跳转到失败页面
        /*
         *   可以设置一个登录的变量，初始记为false，如果一切顺利，登陆成功，就改为true
         *   如果是登录成功，就跳转到成功页面：response.sendRedirect(request.getContextPath() + "/welcome.html");
         *   如果是失败，就跳转到失败页面：response.sendRedirect(request.getContextPath() + "/login_error.html");
         * */
        boolean loginCheck = false;
        Connection conn = null;
        Customer customer;  // 可以先声明，只要不使用就没事，但是如果使用的话，该对象就一定要初始化
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        System.out.println(
                "username == " + username + "\n" +
                        "password == " + password + "\n"
        );
        try {
            conn = dbUtil.getConn();
            System.out.println("customer 数据库连接成功，身份验证成功");
            if (customerDao.isExist(conn, username)) {
                customer = new Customer(username, password);
                System.out.println("账号存在");
                customer = customerDao.login(conn, customer);
                if (customer != null) {
                    System.out.println("登录成功打印输出");
                    loginCheck = true;
                } else {
                    System.out.println("提示密码错误");
                }
            } else {
                System.out.println("提示该用户不存在");
            }
        } catch (ClassNotFoundException e) {
            System.out.println("ClassNotFoundException错误");
            e.printStackTrace();
        } catch (SQLException e) {
            System.out.println("SQLException错误");
            e.printStackTrace();
        } finally {
            try {
                System.out.println("数据库已关闭");
                conn.close();
            } catch (SQLException e) {
                System.out.println("数据库关闭失败");
                e.printStackTrace();
            }
        }
        System.out.println("上下文路径是：" + req.getContextPath());

        StringBuilder json = new StringBuilder();
        if (loginCheck) {
            String checked = req.getParameter("checkbox");
            System.out.println(checked + " ?==" + "checked");
            if (checked != null && checked.equals("checked")) {
//                创建cookie对象
                Cookie cookieUsername = new Cookie("username", username);
                Cookie cookiePassword = new Cookie("password", password);
//                设置有效时间
                cookieUsername.setMaxAge(60 * 60 * 24 * 10);
                cookiePassword.setMaxAge(60 * 60 * 24 * 10);
//                设置关联路径
                cookieUsername.setPath(req.getContextPath());
                cookiePassword.setPath(req.getContextPath());
//                发送Cookie给浏览器
                resp.addCookie(cookieUsername);
                resp.addCookie(cookiePassword);
            }

            HttpSession session = req.getSession();
            session.setAttribute("username", username);
            json.append("{\"success\" : true}");
        } else {
            json.append("{\"success\" : false}");
        }

        resp.setContentType("application/json; charset=utf-8");
        resp.getWriter().print(json);
    }
}